Option Strict Off
Option Explicit On
Module TerminalMod
    'Public Declare Function FT_GetNumberOfDevices Lib "BIKIUSB.DLL" Alias "FT_ListDevices" (ByRef lngNumberOfDevices As Integer, ByVal pvArg2 As String, ByVal lngFlags As Integer) As Integer
    'Public Declare Function FT_GetDeviceString Lib "BIKIUSB.DLL" Alias "FT_ListDevices" (ByVal lngDeviceIndex As Integer, ByVal lpszDeviceString As String, ByVal lngFlags As Integer) As Integer
    'Public Declare Function FT_GetDeviceInfo Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lngFT_Type As Integer, ByRef lngID As Integer, ByVal pucSerialNumber As String, ByVal pucDescription As String, ByRef pvDummy As Byte) As Integer



    ''===========================================================================================================================
    '' Declarations for functions in FTD2XX.dll:
    ''===========================================================================================================================
    'Public Declare Function FT_OpenByIndex Lib "BIKIUSB.DLL" Alias "FT_Open" (ByVal intDeviceNumber As Integer, ByRef lngHandle As Integer) As Integer
    'Public Declare Function FT_OpenBySerialNumber Lib "BIKIUSB.DLL" Alias "FT_OpenEx" (ByVal SerialNumber As String, ByVal lngFlags As Integer, ByRef lngHandle As Integer) As Integer
    'Public Declare Function FT_OpenByDescription Lib "BIKIUSB.DLL" Alias "FT_OpenEx" (ByVal Description As String, ByVal lngFlags As Integer, ByRef lngHandle As Integer) As Integer
    'Public Declare Function FT_Close Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_Read_String Lib "BIKIUSB.DLL" Alias "FT_Read" (ByVal lngHandle As Integer, ByVal lpvBuffer As String, ByVal lngBufferSize As Integer, ByRef lngBytesReturned As Integer) As Integer
    'Public Declare Function FT_Write_String Lib "BIKIUSB.DLL" Alias "FT_Write" (ByVal lngHandle As Integer, ByVal lpvBuffer As String, ByVal lngBufferSize As Integer, ByRef lngBytesWritten As Integer) As Integer
    'Public Declare Function FT_Read_Bytes Lib "BIKIUSB.DLL" Alias "FT_Read" (ByVal lngHandle As Integer, ByRef lpvBuffer As Byte, ByVal lngBufferSize As Integer, ByRef lngBytesReturned As Integer) As Integer
    'Public Declare Function FT_Write_Bytes Lib "BIKIUSB.DLL" Alias "FT_Write" (ByVal lngHandle As Integer, ByRef lpvBuffer As Byte, ByVal lngBufferSize As Integer, ByRef lngBytesWritten As Integer) As Integer
    'Public Declare Function FT_SetBaudRate Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal lngBaudRate As Integer) As Integer
    'Public Declare Function FT_SetDivisor Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal intDivisor As Integer) As Integer
    'Public Declare Function FT_SetDataCharacteristics Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal byWordLength As Byte, ByVal byStopBits As Byte, ByVal byParity As Byte) As Integer
    'Public Declare Function FT_SetFlowControl Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal intFlowControl As Integer, ByVal byXonChar As Byte, ByVal byXoffChar As Byte) As Integer
    'Public Declare Function FT_ResetDevice Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_SetDtr Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_ClrDtr Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_SetRts Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_ClrRts Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_GetModemStatus Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lngModemStatus As Integer) As Integer
    'Public Declare Function FT_SetChars Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal byEventChar As Byte, ByVal byEventCharEnabled As Byte, ByVal byErrorChar As Byte, ByVal byErrorCharEnabled As Byte) As Integer
    'Public Declare Function FT_Purge Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal lngMask As Integer) As Integer
    'Public Declare Function FT_SetTimeouts Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal lngReadTimeout As Integer, ByVal lngWriteTimeout As Integer) As Integer
    'Public Declare Function FT_GetQueueStatus Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lngRxBytes As Integer) As Integer
    'Public Declare Function FT_GetLatencyTimer Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef ucTimer As Byte) As Integer
    'Public Declare Function FT_SetLatencyTimer Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal ucTimer As Byte) As Integer
    'Public Declare Function FT_GetBitMode Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef ucMode As Byte) As Integer
    'Public Declare Function FT_SetBitMode Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal ucMask As Byte, ByVal ucEnable As Byte) As Integer
    'Public Declare Function FT_SetUSBParameters Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal lngInTransferSize As Integer, ByVal lngOutTransferSize As Integer) As Integer
    'Public Declare Function FT_SetBreakOn Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_SetBreakOff Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer) As Integer
    'Public Declare Function FT_GetStatus Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lngamountInRxQueue As Integer, ByRef lngAmountInTxQueue As Integer, ByRef lngEventStatus As Integer) As Integer

    'Public Declare Function FT_SetEventNotification Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal lngEventMask As Integer, ByVal lngEvent As Integer) As Integer

    ''==============================================================
    ''Declarations for the EEPROM-accessing functions in FTD2XX.dll:
    ''==============================================================
    ''UPGRADE_WARNING: Structure FT_PROGRAM_DATA may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'Private Declare Function FT_EE_Program Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lpData As FT_PROGRAM_DATA) As Integer
    ''UPGRADE_WARNING: Structure FT_PROGRAM_DATA may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="C429C3A5-5D47-4CD9-8F51-74A1616405DC"'
    'Private Declare Function FT_EE_Read Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lpData As FT_PROGRAM_DATA) As Integer
    'Private Declare Function FT_EE_UASize Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByRef lpdwSize As Integer) As Integer
    'Private Declare Function FT_EE_UAWrite Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal pucData As String, ByVal dwDataLen As Integer) As Integer
    'Private Declare Function FT_EE_UARead Lib "BIKIUSB.DLL" (ByVal lngHandle As Integer, ByVal pucData As String, ByVal dwDataLen As Integer, ByRef lpdwBytesRead As Integer) As Integer


    '====================================================================
    'UPGRADE_NOTE: object was upgraded to object_Renamed. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
    'UPGRADE_ISSUE: Declaring a parameter 'As Any' is not supported. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="FAE78A8D-8978-4FD4-8208-5B7324A8F795"'
    '====================================================================
    'Type definition as equivalent for C-structure "ft_program_data" used
    'in FT_EE_READ and FT_EE_WRITE;
    'ATTENTION! The variables "Manufacturer", "ManufacturerID",
    '"Description" and "SerialNumber" are passed as POINTERS to
    'locations of Bytearrays. Each Byte in these arrays will be
    'filled with one character of the whole string.
    '(See below, calls to "agGetAddressForObject")
    '=====================================================================
    Private Structure FT_PROGRAM_DATA
        Dim VendorId As Short '0x0403
        Dim ProductId As Short '0x6001
        Dim Manufacturer As Integer '32 "FTDI"
        Dim ManufacturerId As Integer '16 "FT"
        Dim description As Integer '64 "USB HS Serial Converter"
        Dim SerialNumber As Integer '16 "FT000001" if fixed, or NULL
        Dim MaxPower As Short ' // 0 < MaxPower <= 500
        Dim PnP As Short ' // 0 = disabled, 1 = enabled
        Dim SelfPowered As Short ' // 0 = bus powered, 1 = self powered
        Dim RemoteWakeup As Short ' // 0 = not capable, 1 = capable
        ' Rev4 extensions:
        Dim Rev4 As Boolean ' // true if Rev4 chip, false otherwise
        Dim IsoIn As Boolean ' // true if in endpoint is isochronous
        Dim IsoOut As Boolean ' // true if out endpoint is isochronous
        Dim PullDownEnable As Boolean ' // true if pull down enabled
        Dim SerNumEnable As Boolean ' // true if serial number to be used
        Dim USBVersionEnable As Boolean ' // true if chip uses USBVersion
        Dim USBVersion As Short ' // BCD (0x0200 => USB2)
    End Structure

    ' Return codes
    Public Const FT_OK As Short = 0
    Const FT_INVALID_HANDLE As Short = 1
    Const FT_DEVICE_NOT_FOUND As Short = 2
    Const FT_DEVICE_NOT_OPENED As Short = 3
    Const FT_IO_ERROR As Short = 4
    Const FT_INSUFFICIENT_RESOURCES As Short = 5
    Const FT_INVALID_PARAMETER As Short = 6
    Const FT_INVALID_BAUD_RATE As Short = 7

    Const FT_DEVICE_NOT_OPENED_FOR_ERASE As Short = 8
    Const FT_DEVICE_NOT_OPENED_FOR_WRITE As Short = 9
    Const FT_FAILED_TO_WRITE_DEVICE As Short = 10
    Const FT_EEPROM_READ_FAILED As Short = 11
    Const FT_EEPROM_WRITE_FAILED As Short = 12
    Const FT_EEPROM_ERASE_FAILED As Short = 13
    Const FT_EEPROM_NOT_PRESENT As Short = 14
    Const FT_EEPROM_NOT_PROGRAMMED As Short = 15
    Const FT_INVALID_ARGS As Short = 16
    Const FT_OTHER_ERROR As Short = 17

    Const FT_BITS_8 As Short = 8
    Const FT_BITS_7 As Short = 7

    ' Stop Bits
    Const FT_STOP_BITS_1 As Short = 0
    Const FT_STOP_BITS_1_5 As Short = 1
    Const FT_STOP_BITS_2 As Short = 2

    ' Parity
    Const FT_PARITY_NONE As Short = 0
    Const FT_PARITY_ODD As Short = 1
    Const FT_PARITY_EVEN As Short = 2
    Const FT_PARITY_MARK As Short = 3
    Const FT_PARITY_SPACE As Short = 4

    ' Flow Control
    Const FT_FLOW_NONE As Short = &H0S
    Const FT_FLOW_RTS_CTS As Short = &H100S
    Const FT_FLOW_DTR_DSR As Short = &H200S
    Const FT_FLOW_XON_XOFF As Short = &H400S

    ' Purge rx and tx buffers
    Public Const FT_PURGE_RX As Short = 1
    Public Const FT_PURGE_TX As Short = 2

    ' Flags for FT_OpenEx
    Const FT_OPEN_BY_SERIAL_NUMBER As Short = 1
    Const FT_OPEN_BY_DESCRIPTION As Short = 2

    ' Flags for FT_ListDevices
    Const FT_LIST_BY_NUMBER_ONLY As Integer = &H80000000
    Const FT_LIST_BY_INDEX As Integer = &H40000000
    Const FT_LIST_ALL As Integer = &H20000000

	
    'Public lngHandle As Integer
	
    Public Function usb_connect(ByVal f_baud As Integer) As Boolean

        FT_Close(lngHandle)
        If FT_OpenByIndex(0, lngHandle) <> FT_OK Then
            Err_message("Failed openEx")
            GoTo closehandle
        End If
        If FT_SetBaudRate(lngHandle, f_baud) <> FT_OK Then
            Err_message("Failed Baud")
            GoTo CloseHandle
        End If
        If FT_SetDataCharacteristics(lngHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE) <> FT_OK Then
            Err_message("Failed DataCharacteristics")
            GoTo CloseHandle
        End If
        If FT_SetFlowControl(lngHandle, FT_FLOW_NONE, 0, 0) <> FT_OK Then
            Err_message("SetFlowControl Failed")
            GoTo CloseHandle
        End If
        If FT_SetTimeouts(lngHandle, 4000, 0) <> FT_OK Then
            Err_message("SetFlowControl Failed")
            GoTo CloseHandle
        End If
        If FT_SetLatencyTimer(lngHandle, 1) <> FT_OK Then
            Err_message("SetLatency Failed")
            GoTo CloseHandle
        End If
        If FT_SetUSBParameters(lngHandle, 600, 1080) <> FT_OK Then
            GoTo CloseHandle
        End If
        'Terminal.tmr.Enabled = True
        FT_Purge(lngHandle, FT_PURGE_RX + FT_PURGE_TX)
        FT_ClrRts(lngHandle)
        Return True
CloseHandle:
        FT_Close(lngHandle)
        lngHandle = 0
        Return False
    End Function
    '    Public Sub BikiRom_Reset()
    '        FT_SetDtr(lngHandle)
    '        'System.Threading.Thread.Sleep(20)
    '        FT_ClrDtr(lngHandle)
    '    End Sub
    '    Public Function BikiRom_ReInit() As Boolean
    '        Dim s_init() As Byte = {0}
    '        Dim ret(3) As Byte
    '        Dim s_try As Byte = 1
    'retry:
    '        BikiRom_Reset()
    '        System.Threading.Thread.Sleep(1200 * s_try)
    '        FT_Purge(lngHandle, FT_PURGE_RX)
    '        If Not BikiRom_sendCmdd(s_init, ret, 1, 1) Then
    '            System.Threading.Thread.Sleep(200)
    '            FT_Purge(lngHandle, FT_PURGE_RX)
    '            If BikiRom_sendCmdd(s_init, ret, 1, 1) Then
    '                If ret(0) = 170 Then
    '                    Return True
    '                Else
    '                    Return False
    '                End If
    '            Else
    '                Return False
    '            End If
    '        Else
    '            If ret(0) = 170 Then
    '                Return True
    '            Else
    '                If s_try <= 3 Then
    '                    s_try += 1
    '                    GoTo retry
    '                End If
    '                Return False
    '            End If
    '        End If
    '    End Function
    Friend Sub arrayDump(ByRef sArray() As Byte, ByVal sOutFile As String)
        Dim fStream As New IO.FileStream(sOutFile, IO.FileMode.Create)
        'Dim bWrite As New (fStream)
        fStream.Write(sArray, 0, sArray.Length)
        fStream.Close()
    End Sub
    '    Public Function BikiRom_Init() As Boolean
    '        Dim f_retry As Byte = 0
    '        Dim f_Pass As Boolean = False
    '        Dim f_return(0) As Byte
    'beforeConnect:
    '        If usb_connect(Val(Terminal.Baud.Text)) Then
    'beforeInit:
    '            FT_Purge(lngHandle, FT_PURGE_RX)
    '            Debug.Print("connected OK")
    '            If BikiRom_hexCmd("00", f_return, 1) Then
    '                Debug.Print("sent 00 OK and recieved something")
    '                If f_return(0) = 170 Then
    '                    Debug.Print("got a AA ok response")
    '                    f_Pass = True
    '                Else
    '                    Debug.Print("response not AA ok")
    '                    If BikiRom_ReInit() Then
    '                        Debug.Print("re-init OK")
    '                        f_Pass = True
    '                    Else
    '                        Debug.Print("re-init not ok")
    '                        f_Pass = False
    '                        f_retry += 1
    '                        If f_retry <= 10 Then GoTo beforeInit
    '                        GoTo CloseHandle
    '                    End If
    '                End If
    '            Else
    '                If BikiRom_ReInit() Then
    '                    Debug.Print("re-init OK")
    '                    f_Pass = True
    '                Else
    '                    Debug.Print("re-init not ok")
    '                    f_Pass = False
    '                    f_retry += 1
    '                    If f_retry <= 10 Then GoTo beforeInit
    '                    GoTo CloseHandle
    '                End If
    '            End If
    '        Else
    '            Debug.Print("not connected ok")
    '            f_retry += 1
    '            If f_retry <= 10 Then GoTo beforeConnect
    '            GoTo CloseHandle
    '        End If

    '        Return f_Pass
    '        Exit Function
    'CloseHandle:
    '        Debug.Print("failed and disconnected")
    '        Return False
    '        FT_Close(lngHandle)
    '    End Function

	Public Function usb_write(ByRef Send_() As Byte, ByVal sendLen As Integer) As Boolean
		Dim f_retCount As Integer
        If FT_Write_Bytes(lngHandle, Send_(0), sendLen, f_retCount) <> FT_OK Then
            usb_write = False
            'Debug.Print ("FT_Send - send# :" & sendLen & " written: " & f_retCount)
            Exit Function
        Else
            usb_write = True
        End If
	End Function
	
    'Public Function usb_read(ByRef RxBytes As Integer) As Byte()
    '    Dim retBytes(512) As Byte
    '    Dim f_BytesInBuffer As Integer = 0
    '    FT_GetQueueStatus(lngHandle, f_BytesInBuffer)
    '    If f_BytesInBuffer > 0 Then
    '        If f_BytesInBuffer >= 512 Then f_BytesInBuffer = 512
    '        FT_Read_Bytes(lngHandle, retBytes(0), f_BytesInBuffer, RxBytes)
    '    End If
    '    ReDim Preserve retBytes(RxBytes - 1)
    '    Return retBytes
    'End Function
	
    Public Sub usb_close()
        FT_Close(lngHandle)
        lngHandle = 0
    End Sub

    Function hiInt(ByVal w As UInt32) As UInt16
        Return (w And &HFFFF0000) \ 65536
    End Function
    Function loInt(ByVal w As UInt32) As UInt16
        Return w And &HFFFF
    End Function
    Friend Function MAKEWORD(ByVal wLow As Short, ByVal wHigh As Short) As Integer
        Return LOBYTE(wLow) Or (&H100 * LOBYTE(wHigh))
    End Function
    Friend Function MAKEWORD(ByVal wLow As Byte, ByVal wHigh As Byte) As UInt16
        Return wLow Or (&H100 * wHigh)
    End Function
    Friend Function BitSet_byte(ByRef Number As Byte, ByVal Bit As Byte) As Byte
        If Bit = 7 Then
            Number = &H80S Or Number
        Else
            Number = (2 ^ Bit) Or Number
        End If

        BitSet_byte = Number
    End Function
    Friend Function BitIsSet_byte(ByVal Number As Byte, ByVal Bit As Byte) As Boolean
        If Bit = 7 Then
            If Number And &H80S Then Return True
        Else
            If Number And (2 ^ Bit) Then Return True
        End If
        Return False
    End Function
    Public Sub StatusPercent(ByVal perCent As Integer)
        setBar(perCent)
    End Sub
    Public Sub setBar(ByVal perCent As Integer)
        Terminal.prog.Maximum = 100
        Terminal.prog.Minimum = 0
        If (perCent <= 100) And (perCent >= 0) Then
            Terminal.prog.Value = perCent
        End If
    End Sub
    Friend Function makeLong(ByVal MSB As Byte, ByVal LMSB As Byte, ByVal LSB As Byte, ByVal MLSB As Byte) As UInt64
        Return MLSB Or (&H100 * LSB) Or (&H10000 * LMSB) Or (&H1000000L * MSB)
    End Function

    Public Sub Err_message(ByVal str_ As String)
        Terminal.stMessage.Text = str_
    End Sub
    'Public Function BikiRom_hexCmd(ByVal Str_ As String, ByRef Return_() As Byte, ByVal retLen As Integer) As Boolean
    '    Dim f_cmd() As Byte = cv_BytesFromHex(Str_)
    '    If BikiRom_sndCmd(lngHandle, f_cmd, Return_, f_cmd.Length, retLen) Then
    '        Return True
    '    Else
    '        Return False
    '    End If
    'End Function
    'Public Function BikiRom_hexCmd(ByVal Str_ As String, ByVal retLen As Integer) As Boolean
    '    Dim f_cmd() As Byte = cv_BytesFromHex(Str_)
    '    Dim f_return(0 To retLen) As Byte
    '    If BikiRom_sndCmd(lngHandle, f_cmd, f_return, f_cmd.Length, retLen) Then
    '        Return True
    '    Else
    '        Return False
    '    End If
    'End Function
    'Friend Function BikiRom_hexCmd(ByVal inputStr As String, ByVal replyStr As String) As Boolean
    '    Dim f_cmd() As Byte = cv_BytesFromHex(inputStr)
    '    Dim f_reply() As Byte = cv_BytesFromHex(replyStr)
    '    Dim f_return(f_reply.Length - 1) As Byte
    '    Dim f_result As Boolean = True
    '    If BikiRom_sndCmd(lngHandle, f_cmd, f_return, f_cmd.Length, f_reply.Length) = True Then
    '        For i As Integer = 0 To UBound(f_reply)
    '            If f_reply(i) <> f_return(i) Then f_result = False
    '        Next
    '        Return f_result
    '    Else
    '        Return False
    '    End If
    'End Function
    'Public Function BikiRom_hexCmd(ByVal Str_ As String) As Boolean
    '    Dim f_cmd() As Byte = cv_BytesFromHex(Str_)
    '    Dim f_return(0 To 1) As Byte
    '    If BikiRom_sndCmd(lngHandle, f_cmd, f_return, f_cmd.Length, 0) Then
    '        Return True
    '    Else
    '        Return False
    '    End If
    'End Function
    'Public Function BikiRom_sendCmdd(ByRef Send_() As Byte, ByRef Return_() As Byte, ByVal sendLen As Integer, ByVal retLen As Integer) As Boolean
    '    If BikiRom_sndCmd(lngHandle, Send_, Return_, sendLen, retLen) Then
    '        BikiRom_sendCmdd = True
    '    Else
    '        BikiRom_sendCmdd = False
    '    End If
    'End Function
    'Public Function BikiRom_read(ByVal ReturnLen As Integer) As Byte()
    '    Dim return_(ReturnLen - 1) As Byte
    '    Dim f_retCount As Integer = ReturnLen
    '    Dim flTimedout As Boolean = False
    '    Dim flFatalError As Boolean = False
    '    Dim lngTotalBytesRead As Integer = 0
    '    Dim ftStatus As Short
    '    Dim lngBytesRead As Integer = 0
    '    Dim byteArray(258) As Byte
    '    Dim arrayCursor As Short = 0
    '    Dim i As Short
    '    If ReturnLen <> 0 Then
    '        flTimedout = False
    '        flFatalError = False

    '        '******************
    '        '** Wait for number of bytes code
    '        lngTotalBytesRead = 0
    '        Do
    '            lngBytesRead = 0
    '            ftStatus = FT_Read_Bytes(lngHandle, byteArray(0), f_retCount, lngBytesRead)
    '            Debug.Print("FT_Read - send# :" & f_retCount & " Read: " & lngTotalBytesRead)
    '            If (ftStatus = FT_OK) Or (ftStatus = FT_IO_ERROR) Then
    '                If lngBytesRead > 0 Then
    '                    For i = 1 To lngBytesRead
    '                        Return_(arrayCursor) = byteArray(i - 1)
    '                        arrayCursor += 1
    '                    Next
    '                    lngTotalBytesRead += lngBytesRead
    '                    f_retCount -= lngBytesRead
    '                Else
    '                    flTimedout = True
    '                End If
    '            Else
    '                flFatalError = True
    '            End If
    '        Loop Until (lngTotalBytesRead >= ReturnLen) Or (flTimedout = True) Or (flFatalError = True)
    '    Else
    '        FT_Purge(lngHandle, FT_PURGE_RX)
    '    End If
    '    '*******************
    '    '** Return Code
    '    Return Return_
    'End Function
    'Public Function BikiRom_sndCmd(ByRef f_lngHandle As Integer, ByRef Send_() As Byte, ByRef Return_() As Byte, ByVal sendLen As Integer, ByVal retLen As Integer) As Boolean
    '    Dim f_retCount As Integer
    '    Dim flTimedout As Boolean = False
    '    Dim flFatalError As Boolean = False
    '    Dim lngTotalBytesRead As Integer = 0
    '    Dim ftStatus As Short
    '    Dim f_rcvLen As Integer = retLen
    '    Dim lngBytesRead As Integer = 0
    '    Dim byteArray(retLen) As Byte
    '    Dim arrayCursor As Short = 0
    '    Dim i As Short
    '    Dim f_retlen As Integer = retLen

    '    FT_Purge(lngHandle, FT_PURGE_RX + FT_PURGE_TX)
    '    'Debug.Print ("cmd: " & cv_HexFromString(Send_) & " length: " & sendLen)
    '    '********************
    '    '** Send Command Code
    '    If FT_Write_Bytes(f_lngHandle, Send_(0), sendLen, f_retCount) <> FT_OK Then
    '        Return False
    '        Terminal.write_Log("error sending bytes to usb", "ERROR")
    '        'Debug.Print ("FT_Send - send# :" & sendLen & " written: " & f_retCount)
    '        Exit Function
    '    ElseIf retLen <> 0 Then
    '        flTimedout = False
    '        flFatalError = False

    '        '******************
    '        '** Wait for number of bytes code
    '        lngTotalBytesRead = 0
    '        Do
    '            lngBytesRead = 0
    '            ftStatus = FT_Read_Bytes(f_lngHandle, byteArray(0), f_rcvLen, lngBytesRead)
    '            ' Debug.Print("FT_Read - send# :" & retLen & " Read: " & lngTotalBytesRead)
    '            If (ftStatus = FT_OK) Or (ftStatus = FT_IO_ERROR) Then
    '                If lngBytesRead > 0 Then
    '                    For i = 1 To lngBytesRead
    '                        Return_(arrayCursor) = byteArray(i - 1)
    '                        arrayCursor += 1
    '                    Next
    '                    lngTotalBytesRead += lngBytesRead
    '                    f_rcvLen -= lngBytesRead
    '                Else
    '                    flTimedout = True
    '                End If
    '            Else
    '                flFatalError = True
    '            End If
    '        Loop Until (lngTotalBytesRead >= retLen) Or (flTimedout = True) Or (flFatalError = True)
    '    Else
    '        FT_Purge(f_lngHandle, FT_PURGE_RX)
    '    End If
    '    '*******************
    '    '** Return Code
    '    If sendLen >= 1 Then
    '        Dim f_send(0 To sendLen) As Byte
    '        array_copy(Send_, 0, f_send, 0, sendLen)
    '        'Terminal.write_LogBytes(f_send, "Sent OK :")
    '    End If
    '    If lngTotalBytesRead >= 1 Then
    '        Dim f_recv(0 To lngTotalBytesRead) As Byte
    '        array_copy(Return_, 0, f_recv, 0, lngTotalBytesRead)
    '        'Terminal.write_LogBytes(f_recv, "Recv OK :")
    '    End If

    '    'Debug.Print("return: " & cv_HexFromString(Return_) & " bytes: " & lngTotalBytesRead)
    '    If flTimedout = True Or flFatalError = True Then
    '        Return False
    '    Else
    '        Return True
    '    End If
    'End Function
    Public Sub array_copy(ByRef From_() As Byte, ByVal F_Off As Integer, ByRef to_() As Byte, ByVal T_Off As Integer, ByVal Length_ As Integer)
        Dim f_count As Integer
        If Length_ >= 1 Then
            For f_count = 0 To Length_ - 1
                to_(f_count + T_Off) = From_(f_count + F_Off)
            Next
        End If
    End Sub
    Public Function pp(ByRef val As Object) As Object
        Dim ret As Object = val
        val = val + 1
        Return ret
    End Function
    Public Function LOBYTE(ByVal w As Integer) As Byte
        LOBYTE = w And &HFFS
    End Function

    Function HIBYTE(ByVal w As Integer) As Byte
        HIBYTE = (w And &HFF00) \ 256
    End Function
    Public Function BitIsSet(ByVal Number As Byte, ByVal Bit As Byte) As Boolean

        If Bit = 7 Then
            If Number And &H80S Then
                BitIsSet = True
            End If
        ElseIf Number And (2 ^ Bit) Then
            BitIsSet = True
        Else
            BitIsSet = False
        End If
    End Function
End Module